package com.qiaofang.collectmanage.service.common.utils.phone;

import com.alibaba.fastjson.JSON;
import com.qiaofang.collectmanage.service.common.utils.HttpClientUtils;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class GanjiHandler {
    private static byte[] iv = {18, 52, 86, 120, -112, -85, -51, -17};
    private static byte[] key;
    private static final String CIPHER_MODE = "DES/CBC/PKCS5Padding";

    static {
        try {
            key = new String(new BASE64Decoder().decodeBuffer("aS5IM3ZCLHo=")).getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String GANJI_PHONE_EVAL_URL = "https://app.ganji.com/datashare/";

    public static String decrypt(String paramString1)
            throws Exception {
        byte[] paramString11 = new BASE64Decoder().decodeBuffer(paramString1);
        IvParameterSpec localIvParameterSpec = new IvParameterSpec(iv);
        SecretKeySpec paramString22 = new SecretKeySpec(key, "DES");
        Cipher localCipher = Cipher.getInstance(CIPHER_MODE);
        localCipher.init(2, paramString22, localIvParameterSpec);
        return new String(localCipher.doFinal(paramString11));
    }

    public static String getPhone(String houseId) {
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("puid", houseId);

            Map<String, String> headerMap = new HashMap<>();

            headerMap.put("interface", "GetPostByPuid");
            headerMap.put("tk", "704e6d42704e36576f515053626b2f63704e547464777155");
            headerMap.put("unid", "3365d311a0e59fe774c9a72b2cad2afb");
            headerMap.put("ay", "baidustore2");
            headerMap.put("userId", "EAE45955E11007EC4533911AAC9494CF");
            headerMap.put("agency", "baidustore2");
            headerMap.put("lbs", "");
            headerMap.put("uniqueId", "3365d311a0e59fe774c9a72b2cad2afb");
            headerMap.put("ct", "13");
            headerMap.put("versionId", "8.8.5");
            headerMap.put("xadid", "n7RnFsSa3c8l9ADAZ0gBHw+Ya7dJWFJzeK68dW+9+UArBf6eO2L/ghUEUzlLBH8j");
            headerMap.put("ds", "3.0");
            headerMap.put("Token", "704e6d42704e36576f515053626b2f63704e547464777155");
            headerMap.put("sid", "aa3aa714-827b-4b06-8f6c-59d4c56add8d");
            headerMap.put("rl", "1080*1920");
            headerMap.put("clientAgent", "xiaomi/xiaomi 6#1080*1920#3.0#5.1.1");
            headerMap.put("GjData-Version", "1.0");
            headerMap.put("contentformat", "json2");
            headerMap.put("sc", "a6f78e7f");
            headerMap.put("of", "self");
            headerMap.put("isp", "46000");
            headerMap.put("dv", "xiaomi/xiaomi 6");
            headerMap.put("cid", "801");
            headerMap.put("lct", "13");
            headerMap.put("net", "wifi");
            headerMap.put("lng", "121.615809");
            headerMap.put("os", "Android");
            headerMap.put("rid", "95e28abe-54d6-4f96-ad48-c1d405f4639f");
            headerMap.put("ov", "22");
            headerMap.put("vs", "8.8.5");
            headerMap.put("uid", "687778731");
            headerMap.put("lar", "");
            headerMap.put("lat", "31.21562");
            headerMap.put("aid", "EAE45955E11007EC4533911AAC9494CF");
            headerMap.put("CustomerId", "801");
            headerMap.put("model", "xiaomi/xiaomi 6");
            headerMap.put("Content-Type", "application/x-www-form-urlencoded");
            headerMap.put("Accept", "*/*");
            headerMap.put("Accept-Encoding", "gzip");
            headerMap.put("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 5.1.1; xiaomi 6 Build/LMY48Z)");
            headerMap.put("Host", "app.ganji.com");
            headerMap.put("Connection", "Keep-Alive");


            String response = HttpClientUtils.doHttpPost(GANJI_PHONE_EVAL_URL, paramMap, headerMap, "utf-8");
            String encryptPhone = JSON.parseObject(response).getJSONObject("data").getString("phone");
            return decrypt(encryptPhone);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


}
